﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AYA.Core.Config
{
    public class Configurator
    {
        private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static Configurator _current;
        private System.Xml.XmlDocument _xml=null;
        string configFilePath;

        private Configurator()
        {
            
            configFilePath = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["ConfigFile"]) ? string.Empty : System.Configuration.ConfigurationManager.AppSettings["ConfigFile"];
            if (string.IsNullOrEmpty(configFilePath))
            {
                if (System.Web.HttpContext.Current != null)
                    configFilePath = System.Web.HttpContext.Current.Server.MapPath(configFilePath);                
            }
            if (!System.IO.File.Exists(configFilePath))
                log.Error("找不到配置文件:" + configFilePath);
            else
            {
                _xml=new System.Xml.XmlDocument();
                _xml.Load(configFilePath);
            }
        }

        
        public Configurator Current
        {
            get
            {
                _current = AYA.Core.Cache.CacheManager.Current.Get("__config") as Configurator;
                if (_current == null)
                {
                    _current = new Configurator();
                    AYA.Core.Cache.CacheManager.Current.Add("__config", _current, -1, -1, configFilePath);
                }
                return _current;
            }
        }
        public string this[string section, string key]
        {
            get
            {
                System.Xml.XmlNode n = _xml.SelectSingleNode("//"+section+"/"+key);
                if (n != null)
                    return n.Value;
                return "";
            }
        }
    }
}
